Data Structures

What is a B-Tree in Data Structure? | Data Structure Tutorial

What is a B-Tree in Data Structure? | Data Structure Tutorial

What is a B-Tree in Data Structure? | Data Structure Tutorial

In this article, you will understand the B-Tree and its operations.

B-tree is a unique type of self-branching search tree in which each node can have more than one key and can have more than two children. It is a more generalized form of the binary search tree. It is also called a height-balanced m-way tree.

What is the user of the B-tree data structure?

People feel the use of B-tree when they need less time in accessing physical storage media like a hard disk. The secondary storage devices have larger capacities and are slower. Due to this reason, the need for this type of data structure arose from which it can minimize disk access. If we talk about other data structures such as search tree, AVL tree, red-black tree, etc can keep only one key in one node. If they want to store a large number of keys, then the height of the tree becomes very large due to which access time also increases. B-tree can store multiple keys in a node and that particular node can have multiple child nodes. This decreases the height and allows faster disk access.

B-tree Properties:

  1. For each node i, the keys are always stored in increasing order.
  2. In every node, there is a boolean value i.leaf which is true if i is a leaf node.
  3. If the order of the tree is n, then each internal node can contain at most n-1 keys along with a pointer to each child. 
  4. Apart from the root node, each node has at most n children and at least n/2 children.
  5. The height of the tree is the same. 
  6. The root has at least 2 children and contains a minimum of 1 key. 
  7. If n>=1, then for any n-key B-tree of height h and minimum degree t>=2, h>=logt(n+1)/2.

Operations on a B-Tree

Searching an element in a B-Tree

Searching for an element in a B-tree is very similar to searching for an element in a Binary search tree. The following steps are:

  1. Starting from the root node, compare x with the start key of the node. If k = the start key of the node, return the node and the index.
  2. If k.leaf = true, return NULL. It means the element is not found. 
  3. If k < the start key of the root node, search the left child of this particular key. 
  4. If there are more keys in the present node and k > the start node, compare k with the next key in the node. But if k < concurrent key, search the left child of this key or else search the right of the key.
  5. Perform steps 1 to 4 again and again until the leaf is reached.

Applications of B-Tree

  • It is useful in multilevel indexing.
  • Maintaining the databases and file systems.
  • To store blocks of data.
Yamini  Sharma

I am working as a professional engineer in "HIghRadius Technologies Pvt. Ltd." Passionate about learning and exploring new technologies. Enthusiast in writing and sharing what I have in my knowledge bucket.